package com.forlove.app.qselect.school;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.forlove.app.R;
import com.forlove.app.common.widget.dialog.AnimationType;
import com.forlove.app.common.widget.recyview.BaseRecyclerAdapter;
import com.forlove.app.qselect.school.adapter.SelectYuanxiAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择院系
 * Created by yjl on 2018/1/3.
 */

public class YuanxiDialog extends Dialog {
    private Window mWindow;
    private FrameLayout view_back;
    private RecyclerView list_yuanxi;
    private SelectYuanxiAdapter selectYuanxiAdapter;
    private List<String> yuanxi = new ArrayList<>();
    private SelectYuanxiListener selectYuanxiListener;
    protected int[] mAnimations = {R.style.QYQ_Dialog_Animation_Fly,
            R.style.QYQ_Dialog_Animation_Fade,
            R.style.QYQ_Toast_Animation_Fly,
            R.style.QYQ_Toast_Animation_Fade,
            R.style.QYQ_Dialog_Animation_FlyInFadeOut,
            R.style.QYQ_Dialog_Animation_FlyInFlyOut,
            R.style.QYQ_Toast_Animation_FlyInFlyOut,
            R.style.QYQ_Toast_Animation_FlyTopInFlyTopOut,
            R.style.QYQ_Dialog_Animation_RightInRightOut};

    public YuanxiDialog(@NonNull Context context, SelectYuanxiListener yuanxiListener) {
        super(context, R.style.showQRCDialog);
        View rootView = getLayoutInflater().inflate(R.layout.dialog_select_yuanxi, null);
        setContentView(rootView);
        initDialog();
        view_back = rootView.findViewById(R.id.view_back);
        view_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        list_yuanxi = rootView.findViewById(R.id.list_yuanxi);
        selectYuanxiAdapter = new SelectYuanxiAdapter(getContext(), R.layout.item_yuanxi_name, yuanxi);
        list_yuanxi.setLayoutManager(new LinearLayoutManager(getContext()));
        list_yuanxi.setAdapter(selectYuanxiAdapter);
        this.selectYuanxiListener = yuanxiListener;
        selectYuanxiAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (selectYuanxiListener != null) {
                    selectYuanxiListener.selectYuanxi(selectYuanxiAdapter.getPositionData(position));
                    dismiss();
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        selectYuanxiAdapter.hideEmptyView();
        selectYuanxiAdapter.hideFooterView();
    }

    private void initDialog() {
        mWindow = getWindow();
        mWindow.setWindowAnimations(mAnimations[AnimationType.DIALOG_RIGHT_IN_RIGHT_OUT.getValue()]);
        WindowManager.LayoutParams params = mWindow.getAttributes();
        params.gravity = Gravity.BOTTOM | Gravity.CENTER;
        WindowManager windowManager = mWindow.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        params.width = display.getWidth();
        mWindow.setAttributes(params);
    }

    public void clearData() {
        if (yuanxi != null) {
            yuanxi.clear();
        }
        selectYuanxiListener = null;
    }

    public void updateYuanxi(List<String> yuanxi) {
        this.yuanxi.clear();
        this.yuanxi.addAll(yuanxi);
        if (selectYuanxiAdapter != null) {
            selectYuanxiAdapter.updateDatas(yuanxi);
        }
        show();
    }

    public interface SelectYuanxiListener {
        void selectYuanxi(String yuanxi);
    }
}
